/*
 * @Copyright: Citylife 2021
 * @Author: WangXinnong
 * @Date: 2020-12-14 12:48:48
 * @Description: DeviceInfo
 */
import 'dart:ui';

import 'package:device_info/device_info.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:package_info/package_info.dart';

class DeviceInfo {
  final String _xiaomi = "xiaomi";
  final String _huawei = "huawei";
  final String _meizu = "meizu";
  final String _oppo = "oppo";
  final String _vivo = "vivo";
  final String _getui = "getui";

  Size windowSize;
  double safeBottom = 0;
  double safeTop = 0;
  double safeLeft = 0;
  double safeRight = 0;
  bool initialized = false;
  bool isIphoneX = false;
  factory DeviceInfo() => _getInstance();
  static DeviceInfo get instance => _getInstance();
  static DeviceInfo _instance;
  DeviceInfo._internal();

  static DeviceInfo _getInstance() {
    if (_instance == null) {
      _instance = new DeviceInfo._internal();
    }
    return _instance;
  }

  bool _isBrandXiaoMi(AndroidDeviceInfo androidInfo) {
    return _xiaomi == (androidInfo.brand) ||
        _xiaomi == androidInfo.manufacturer;
  }

  bool _isBrandHuawei(AndroidDeviceInfo androidInfo) {
    return _huawei == androidInfo.brand || _huawei == androidInfo.manufacturer;
  }

  bool _isBrandMeizu(AndroidDeviceInfo androidInfo) {
    return _meizu == androidInfo.brand ||
        _meizu == androidInfo.manufacturer ||
        "22c4185e" == androidInfo.brand;
  }

  bool _isBrandOppo(AndroidDeviceInfo androidInfo) {
    return _oppo == androidInfo.brand || _oppo == androidInfo.manufacturer;
  }

  bool _isBrandVivo(AndroidDeviceInfo androidInfo) {
    return _vivo == androidInfo.brand || _vivo == androidInfo.manufacturer;
  }

  String _getVendor(AndroidDeviceInfo androidInfo) {
    if (_isBrandXiaoMi(androidInfo)) {
      return _xiaomi;
    }

    if (_isBrandHuawei(androidInfo)) {
      return _huawei;
    }

    if (_isBrandMeizu(androidInfo)) {
      return _meizu;
    }

    if (_isBrandOppo(androidInfo)) {
      return _oppo;
    }

    if (_isBrandVivo(androidInfo)) {
      return _vivo;
    }

    return _getui;
  }

  String systemVersion;
  String systemName;
  String platform;
  String vendor;
  String deviceId;
  String packageName;
  String packageVersion;
  Future<void> init() async {
    if (systemVersion == null) {
      DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
      if (defaultTargetPlatform == TargetPlatform.iOS) {
        IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
        systemVersion = iosInfo.systemVersion;
        systemName = iosInfo.systemName;
        platform = "iOS";
        deviceId = iosInfo.identifierForVendor;
      } else if (defaultTargetPlatform == TargetPlatform.android) {
        AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
        platform = "Android";
        systemName = "1";
        vendor = _getVendor(androidInfo);
        deviceId = androidInfo.androidId;
      } else {
        systemVersion = "1.0.0";
        systemName = "web";
        platform = "iOS";
        deviceId = "461355d9-b3ac";
      }
      if ((defaultTargetPlatform == TargetPlatform.iOS) ||
          (defaultTargetPlatform == TargetPlatform.android)) {
        PackageInfo packageInfo = await PackageInfo.fromPlatform();
        packageName = packageInfo.packageName;
        packageVersion = packageInfo.version;
      } else {
        packageName = 'test';
        packageVersion = '1.0.0';
      }
    }
  }

  void updateDevice(BuildContext context, bool force) {
    if (!this.initialized || force) {
      MediaQueryData mediaQueryData = MediaQuery.of(context);
      windowSize = mediaQueryData.size;
      safeBottom = mediaQueryData.padding.bottom;
      safeTop = mediaQueryData.padding.top;
      safeLeft = mediaQueryData.padding.left;
      safeRight = mediaQueryData.padding.right;
      this.isIphoneX = safeBottom > 0;
      print("我被重新设置了DeviceInfo$windowSize, safe:$safeTop $safeBottom");
      this.initialized = windowSize.height > 0;
    }
  }
}
